<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.type"
        placeholder="日志类型"
        clearable
        style="width: 120px"
        class="filter-item"
      >
        <el-option
          v-for="item in typeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-date-picker
        v-model="listQuery.dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item"
        style="width: 360px"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="操作时间"
        prop="createTime"
        align="center"
        width="180"
      />
      <el-table-column
        label="操作人"
        prop="username"
        align="center"
        width="120"
      />
      <el-table-column
        label="IP地址"
        prop="ip"
        align="center"
        width="130"
      />
      <el-table-column
        label="日志类型"
        prop="type"
        align="center"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="row.type === 'ERROR' ? 'danger' : 'info'">
            {{ row.type }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作内容"
        prop="operation"
        align="center"
      />
      <el-table-column
        label="请求方法"
        prop="method"
        align="center"
        width="150"
      />
      <el-table-column
        label="请求参数"
        prop="params"
        align="center"
        width="300"
      >
        <template #default="{row}">
          <el-tooltip
            v-if="row.params"
            class="item"
            effect="dark"
            :content="row.params"
            placement="top"
          >
            <span>{{ row.params }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default defineComponent({
  name: 'SystemLog',
  components: { Pagination },
  directives: { waves },
  setup() {
    const list = ref([])
    const total = ref(0)
    const listLoading = ref(true)
    const listQuery = ref({
      page: 1,
      limit: 20,
      keyword: '',
      type: undefined,
      dateRange: []
    })

    const typeOptions = [
      { label: '操作日志', value: 'OPERATION' },
      { label: '错误日志', value: 'ERROR' },
      { label: '登录日志', value: 'LOGIN' }
    ]

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取系统日志数据
      setTimeout(() => {
        list.value = [
          {
            createTime: '2024-01-01 12:00:00',
            username: 'admin',
            ip: '127.0.0.1',
            type: 'OPERATION',
            operation: '查询用户列表',
            method: 'GET',
            params: '{"page":1,"limit":20}'
          }
        ]
        total.value = 1
        listLoading.value = false
      }, 500)
    }

    const handleFilter = () => {
      listQuery.value.page = 1
      getList()
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      total,
      listLoading,
      listQuery,
      typeOptions,
      handleFilter
    }
  }
})
</script>